package cn.dgiot.fd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dgiot.fd.dao.IDeviceUpgradeFlowDAO;
import cn.dgiot.fd.model.DeviceStateModel;
import cn.dgiot.fd.model.DeviceUpgradeFlowModel;
import cn.dgiot.fd.service.IDeviceStateService;
import cn.dgiot.fd.service.IDeviceUpgradeFlowService;

/**
 * [设备升级流水]
 * @author
 *
 */
@Service
public class DeviceUpgradeFlowServiceImpl implements IDeviceUpgradeFlowService{
 
	@Autowired
	private IDeviceUpgradeFlowDAO deviceUpgradeFlowDAO;

	@Autowired
	private IDeviceStateService deviceStateService;
	
	@Override
	public void insertDeviceUpgrade(DeviceUpgradeFlowModel deviceUpgradeFlowModel) {
		List<String> deviceNames = deviceUpgradeFlowModel.getDeviceNames();
		List<DeviceUpgradeFlowModel> deviceUpgradeModelList = new ArrayList<DeviceUpgradeFlowModel>();
		for(int i=0;i<deviceNames.size();i++) {
			String deviceName = deviceNames.get(i);
			DeviceStateModel deviceStateModel = new DeviceStateModel();
	    	deviceStateModel.setDeviceName(deviceName);
	    	deviceStateModel.setProductKey(deviceUpgradeFlowModel.getProductKey());
	    	DeviceStateModel deviceStateModelResult = deviceStateService.getDeviceState(deviceStateModel);
			DeviceUpgradeFlowModel param = new DeviceUpgradeFlowModel();
			param.setIotId(deviceStateModelResult.getIotId());
			param.setDeviceName(deviceName);
			param.setProductKey(deviceUpgradeFlowModel.getProductKey());
			param.setJobId(deviceUpgradeFlowModel.getJobId());
			param.setStatus(deviceUpgradeFlowModel.getStatus());
			param.setCreateTime(deviceUpgradeFlowModel.getCreateTime());
			param.setId(UUID.randomUUID().toString().replace("-", ""));
			deviceUpgradeModelList.add(param);
		} 
		deviceUpgradeFlowDAO.insertDeviceUpgrade(deviceUpgradeModelList);
	}

	@Override
	public void updateDeviceUpgradeStatus(DeviceUpgradeFlowModel deviceUpgradeFlowModel) {
		// TODO Auto-generated method stub
		deviceUpgradeFlowDAO.updateDeviceUpgradeStatus(deviceUpgradeFlowModel);
	}

}
